/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package negocio;

import java.sql.SQLException;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import mapeamento.MapperDTO;

/**
 *
 * @author Angelo
 */
public class FacadeBusiness {
    private MapperDTO map;
    private Owner o;

    //precisa instância única??   
    public FacadeBusiness() {
        this.map = new MapperDTO();
    }
    
    public void connect(String usuario, String senha, String host, int porta, String sid) throws SQLException, ClassNotFoundException{
        map.connect(usuario, senha, host, porta, sid);
    }
    
    public List<String> getOwners() throws SQLException{
        return map.getOwners();
    }
    
    public Owner getOwner(){
        return this.o;
    }
    
//    public void setOwner(String owner) throws SQLException{
//        o = new Owner(owner);
//        o.setTable_list(map.carregaDados(owner));          
//    }    
    
    public boolean createStructure() throws SQLException
    {
        return map.createStructure();
    }
    
    public boolean reCreateStructure(String owner) throws SQLException
    {
        return map.reCreateStructure(owner);
    }
    
    public boolean hasStructure(String owner) throws SQLException
    {
        return map.hasStructure(owner);
    }
    
    public void setOwner(String owner) throws SQLException
    {
        o = new Owner(owner);
        
        map.setOwner(owner);
        
        o.setTable_list(map.loadData(owner));          
    }
    
    public TableModel getTables(){
        DefaultTableModel m = new DefaultTableModel();
        String[] c = {"Tabela", "Registros", "Crescimento", "Retenção"};
        m.setColumnIdentifiers(c);
        Object[] d = new Object[4];

        for (Table t: o.getTable_list()) {
            d[0] = t.getTable_name();
            d[1] = t.getPar_records();
            d[2] = t.getPar_growth_rate();
            d[3] = t.getPar_retention_time();
            m.addRow(d);
        }

        return m;
    }        
      
    public TableModel getColumns(String table_name){        
        DefaultTableModel m = new DefaultTableModel();
        String[] c = {"Coluna", "Tipo", "Tamanho", 
            "Precisão","Aceita nulo", "Tamanho médio", "Percentual nulo"};
        m.setColumnIdentifiers(c);
        Object[] d = new Object[7];
        for (Column col: o.getTable(table_name).getColumn_list()){                                    
            d[0] = col.getColumn_name();
            d[1] = col.getData_type();
            d[2] = col.getData_length();
            d[3] = col.getData_precision();
            d[4] = col.getNullable();
            d[5] = col.getPar_avg_size();
            d[6] = col.getPar_percent_null();           
            m.addRow(d);
        }
        
        return m;                        
    }

    public void updateTable(String table, int records, double growth, int retention) throws Exception{      
        o.updateTable(table, records, growth, retention);      
    }
    
    public void updateColumn(String table, String column, double avgSize, double percNull) throws Exception{       
        o.updateColumn(table, column, avgSize, percNull);                                                   
    }
    
    public void writeData() throws SQLException
    {
        for(Table t: o.getTable_list())
        {
            map.writeDataTables(o.getOwner(), t.getTable_name(), t.getPar_records(), t.getPar_growth_rate(), t.getPar_retention_time());
            
            for(Column c: t.getColumn_list())
            {
                map.writeDataColumn(o.getOwner(), t.getTable_name(), c.getColumn_name(), c.getPar_avg_size(), c.getPar_percent_null());
            }
        }
        
        map.cleanTablesOfValumetricCalculation();
        for(Table t: o.getTable_list())
        {
            map.calcInitialOcupation(t.getTable_name(), t.getPar_records());
        }
    }
          
    public ResultCalcDataBase restultCalc() throws SQLException
    {
        ResultCalcDataBase resultDataBase = new ResultCalcDataBase();
        List<String> tempResultTables = map.restultCalc();
        String[] v;
        for (String tempResultTable : tempResultTables) 
        {
            v = tempResultTable.split(";");
            
            resultDataBase.addTable
                                    (
                                        new ResultCalcTable
                                        (
                                            v[0], 
                                            Double.parseDouble(v[1]), 
                                            Double.parseDouble(v[2]),
                                            Double.parseDouble(v[3]), 
                                            Double.parseDouble(v[4]), 
                                            Double.parseDouble(v[5]), 
                                            Double.parseDouble(v[6])
                                        ));
        }
        return resultDataBase;
    }
}
